package com.quectel.core.module.application.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.quectel.constant.global.SystemConstants;
import com.quectel.core.constants.RedisCacheConstants;
import com.quectel.core.module.application.dao.ApplicationAccessTemplateDao;
import com.quectel.core.module.application.dto.ApplicationAccessTemplateDto;
import com.quectel.core.module.application.entity.ApplicationAccessTemplateEntity;
import com.quectel.core.module.application.service.ApplicationAccessConfService;
import com.quectel.core.module.application.service.ApplicationAccessTemplateService;
import com.quectel.core.module.device.service.DeviceTypeService;
import com.quectel.util.common.CopyUtils;
import com.quectel.util.kit.CacheKit;
import com.quectel.util.kit.Snowflake;
import com.quectel.util.mybatis.MyBatisPlusUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * @author rananxin
 * @date 2021-11-09 09:48:18
 */
@DubboService
public class ApplicationAccessTemplateServiceImpl implements ApplicationAccessTemplateService {

    private static final Logger LOGGER = LoggerFactory.getLogger(ApplicationAccessTemplateServiceImpl.class);

    private static final String APPLICATION_ACCESS_CONF_CACHE_BY_ID = RedisCacheConstants.PROJECT_NAME + "APPLICATION_ACCESS_CONF_CACHE_BY_ID:";

    @Autowired
    private ApplicationAccessTemplateDao applicationAccessTemplateDao;
    @Autowired
    private DeviceTypeService deviceTypeService;
    @Autowired
    private ApplicationAccessConfService applicationAccessConfService;

    @Override
    public ApplicationAccessTemplateDto selectByTypeAndConf(Long typeId, Long accessConfId) {
        ApplicationAccessTemplateEntity applicationAccessTemplateEntity = applicationAccessTemplateDao.selectOne(new LambdaQueryWrapper<ApplicationAccessTemplateEntity>()
                .eq(ApplicationAccessTemplateEntity::getDeviceTypeId, typeId)
                .eq(ApplicationAccessTemplateEntity::getAccessConfId, accessConfId));
        return CopyUtils.copyObj(applicationAccessTemplateEntity, ApplicationAccessTemplateDto.class);
    }

    @Override
    public ApplicationAccessTemplateDto selectCacheById(Long id) {
        return CacheKit.cacheToRedis(
                () -> selectById(id),
                APPLICATION_ACCESS_CONF_CACHE_BY_ID + id,

                SystemConstants.NOT_NULL_CACHE_EXPIRE_SECONDS
        );
    }

    @Override
    public ApplicationAccessTemplateDto selectById(Long id) {
        ApplicationAccessTemplateEntity entity = applicationAccessTemplateDao.selectById(id);
        return paddingField(CopyUtils.copyObj(entity, ApplicationAccessTemplateDto.class));
    }

    @Override
    public List<ApplicationAccessTemplateDto> queryList(Map<String, Object> params) {
        List<ApplicationAccessTemplateEntity> list = applicationAccessTemplateDao.queryList(params);
        List<ApplicationAccessTemplateDto> result = CopyUtils.copyList(list, ApplicationAccessTemplateDto.class);
        result.forEach(this::paddingField);
        return result;
    }

    private ApplicationAccessTemplateDto paddingField(ApplicationAccessTemplateDto dto) {
        if (dto != null) {
            dto.setDeviceType(deviceTypeService.selectById(dto.getDeviceTypeId()));
            dto.setApplicationAccessConf(applicationAccessConfService.selectCacheById(dto.getAccessConfId()));
        }
        return dto;
    }

    @Override
    public int queryTotal(Map<String, Object> params) {
        return applicationAccessTemplateDao.queryTotal(params);
    }

    @Override
    public Long save(ApplicationAccessTemplateDto dto) {
        ApplicationAccessTemplateEntity entity = CopyUtils.copyObj(dto, ApplicationAccessTemplateEntity.class);
        entity.setId(Snowflake.nextId());
        applicationAccessTemplateDao.insert(entity);
        return entity.getId();
    }

    @Override
    public void updateById(ApplicationAccessTemplateDto dto) {
        ApplicationAccessTemplateEntity entity = CopyUtils.copyObj(dto, ApplicationAccessTemplateEntity.class);
        applicationAccessTemplateDao.updateById(entity);
        CacheKit.invalidRedisCache(APPLICATION_ACCESS_CONF_CACHE_BY_ID + dto.getId());
    }

    @Override
    public void updateAllColumnById(ApplicationAccessTemplateDto dto) {

        ApplicationAccessTemplateEntity entity = CopyUtils.copyObj(dto, ApplicationAccessTemplateEntity.class);


        MyBatisPlusUtils.updateAllColumnById(entity, applicationAccessTemplateDao);

        CacheKit.invalidRedisCache(APPLICATION_ACCESS_CONF_CACHE_BY_ID + dto.getId());
    }

    @Override
    public void deleteById(Long id) {
        applicationAccessTemplateDao.deleteById(id);
        CacheKit.invalidRedisCache(APPLICATION_ACCESS_CONF_CACHE_BY_ID + id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBatch(Long[] ids) {
        for (Long id : ids) {
            deleteById(id);
        }
    }
}
